<?php

/**
 * Model MyARContent
 *
 * @author <KTD SOFT>
 * @since Jan 17, 2013 - 3:29:35 PM
 */
class MyARContent extends MyAR
{

    const STATUS_PUBLIC    = 1;
    const STATUS_DRAFT     = 0;
    const STATUS_HIGHLIGHT = 2;

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        $table = $this->getTableName();
        return array(
            'created'  => array(
                self::HAS_ONE,
                'Activity',
                'object_id',
                'condition' => "type='create' AND object_type='$table'",
            ),
            'updated'  => array(
                self::HAS_ONE,
                'Activity',
                'object_id',
                'condition' => "type='update' AND object_type='$table'",
            ),
            'language' => array(
                self::BELONGS_TO,
                'Language',
                'language_id',
                'condition' => "status!=0",
            ),
        );
    }

    public function beforeSave()
    {
        if (parent::beforeSave())
        {
//            if ($this->hasAttribute("language_id"))
//                $this->language_id = Yii::app()->controller->lang_id;
            return TRUE;
        }
        else
            return FALSE;
    }

    public static function getArrayStatus()
    {
        return array(
            self::STATUS_DRAFT     => Yii::t('myarcontent', 'Draft'),
            self::STATUS_PUBLIC    => Yii::t('myarcontent', 'Public'),
            self::STATUS_HIGHLIGHT => Yii::t('myarcontent', 'Highlight'),
        );
    }

    public static function getArrayGoogle()
    {
        return array(
            self::STATUS_DRAFT  => 'No Index',
            self::STATUS_PUBLIC => 'Index',
        );
    }

    public function getStatusLabel()
    {
        if (!$this->hasAttribute("status"))
            return '';

        $labelClass = 'label ';
        switch ($this->status)
        {
            case self::STATUS_PUBLIC:
                $labelClass .= 'label-info';
                break;

            case self::STATUS_HIGHLIGHT:
                $labelClass .= 'label-success';
                break;

            default:
                break;
        }
        $labels = self::getArrayStatus();
        $label  = isset($labels[$this->status]) ? $labels[$this->status] : $this->status;
        return "<span class='$labelClass'>$label</span>";
    }

    public function getPublicUrl()
    {
        return '';
    }

    /* Ham Crop Anh Theo kick Thuoc Dinh San */

//    public function resizePhoto($img, $width, $height)
//    {
////        return 'xxx'.Yii::app()->basePath.'/'.Yii::getPathOfAlias('www') ;
//        
//        $path="/home/admin/stylemart.vn";
//        
//        $original_dir = 'files';
//        $output_dir = 'files/cache';
//
//        $original_path = $path. "/$original_dir/";
//        $base_image_path = str_replace($original_path, "", $img);
//
////        return $base_image_path.'----'.$img;
//        
//        
//        $original_file = $path."$base_image_path";
//        if (!file_exists($original_file) OR $img == NULL)
//        {
//            $base_image_path = 'no_thumbnail.png';
//            $original_file = "$original_dir/$base_image_path";
//        }
//
//        $path_parts = pathinfo($original_file);
//        $extesion = $path_parts['extension'];
//        $f_name = $path_parts['filename'];
//        
//        //
//        $output_file = $path."/$output_dir/{$width}x{$height}/$f_name.$extesion";
//
//        //output_dir=cache;
//        
////        return 'xxx'.$output_dir;
//        
//        // Neu chua ton tai forder thi tao
//        if (!is_dir(($path."/$output_dir/")))
//        {
//            if(mkdir($path."/$output_dir/", 0777, true)){
//                return 'make dir succ';
//            }else{
//                return 'make dir error';
//            }
//        }
//        
//        
//        // Neu Khong ton tai anh thi crop anh
//        if (!file_exists($output_file))
//        {
////        echo $original_file.' '.$output_file;die;
//            $thumb = Yii::app()->phpThumb->create($original_file);
//            $thumb->adaptiveResize($width, $height);
//            if($thumb->save($output_file)){
////                return 'ok';
//            }else{
//                return 'jjj';
//            }
//        }else{
//            return 'uuu';
//        }
//echo $output_file;die;
//        
////        return 'xxx';
//        return  $output_file;
//    }

    public function resizePhoto($img, $width, $height)
    {
        $original_dir = 'files';
        $output_dir   = 'cache';

        $original_path   = Yii::app()->baseUrl . "/$original_dir/";
        $base_image_path = str_replace($original_path, "", $img);

        $original_file = "$original_dir/$base_image_path";
        if (!file_exists($original_file) OR $img == NULL)
        {
            $base_image_path = 'no_thumbnail.png';
            $original_file   = "$original_dir/$base_image_path";
        }

        $output_file = "$output_dir/{$width}x{$height}/$base_image_path";

        // Neu chua ton tai forder thi tao
        if (!is_dir(dirname($output_file)))
        {
            @mkdir(dirname($output_file), 0777, true);
        }

        // Neu Khong ton tai anh thi crop anh
        if (!file_exists($output_file))
        {
            $thumb = Yii::app()->phpThumb->create($original_file);
            $thumb->adaptiveResize($width, $height);
            $thumb->save($output_file);
        }
        return Yii::app()->baseUrl . "/" . $output_file;
    }

    public function resizePhotoByWidth($img, $width)
    {
        $original_dir = 'files';
        $output_dir   = 'cache';

        $original_path   = Yii::app()->baseUrl . "/$original_dir/";
        $base_image_path = str_replace($original_path, "", $img);

        $original_file = "$original_dir/$base_image_path";
        if (!file_exists($original_file) OR $img == NULL)
        {
            $base_image_path = 'no_thumbnail.png';
            $original_file   = "$original_dir/$base_image_path";
        }

        $output_file = "$output_dir/{$width}/$base_image_path";

        // Neu chua ton tai forder thi tao
        if (!is_dir(dirname($output_file)))
        {
            @mkdir(dirname($output_file), 0777, true);
        }

        // Neu Khong ton tai anh thi crop anh
        if (!file_exists($output_file))
        {
            $thumb = Yii::app()->phpThumb->create($original_file);
            $thumb->resizeByWidth($width);
            $thumb->save($output_file);
        }
        return Yii::app()->baseUrl . "/" . $output_file;
    }

}

/** 
 * End file MyARContent
 */
